package com.leetcode.No1779;

import org.junit.Test;

/**
 * @program: Solution
 * @description: 找到最近的有相同 X 或 Y 坐标的点
 * @author: Wang Zhihua
 * @date: 2022-12-01
 */
public class Solution {
    public int nearestValidPoint(int x, int y, int[][] points) {
        int min = Integer.MAX_VALUE;
        int result = -1;
        for (int i = 0; i < points.length; i++) {
            int a = points[i][0];
            int b = points[i][1];
            if (x == a || y == b) {
                int value = Math.abs(x - a) + Math.abs(y - b);
                if (min > value) {
                    min = value;
                    result = i;
                }
            }
        }
        if (min == Integer.MAX_VALUE) {
            result = -1;
        }
        return result;
    }

    @Test
    public void test01() {
        int x, y;
        int[][] points;

        x = 3;
        y = 4;
        points = new int[][]{{1,2}, {3,1}, {2,4}, {2,3}, {4,4}};
        System.out.println(nearestValidPoint(x, y, points));

        x = 3;
        y = 4;
        points = new int[][]{{3,4}};
        System.out.println(nearestValidPoint(x, y, points));
    }
}
